package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.MarketCollect;
import com.cskaoyan.market.db.domain.MarketCollectExample;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.mapper.MarketCollectMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.service.wx.WxCollectService;
import com.cskaoyan.market.vo.wx.lhh.CollectListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName WxCollectServiceImpl
 * @Description
 * @Author MZ
 * @Date 2023/5/22 20:58
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;


    @Override
    public List<CollectListVo> collectList(Byte type, Integer page, Integer limit, Integer userId) {
        List<CollectListVo> result = new ArrayList<>();
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.or().andDeletedEqualTo(false).andTypeEqualTo(type).andUserIdEqualTo(userId);
        List<MarketCollect> collectList = collectMapper.selectByExample(collectExample);
        for (MarketCollect collect : collectList) {
            CollectListVo collectVo = new CollectListVo();
            Integer goodsId = collect.getValueId();
            MarketGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
            collectVo.setBrief(goods.getBrief());
            collectVo.setPicUrl(goods.getPicUrl());
            collectVo.setValueId(goodsId);
            collectVo.setName(goods.getName());
            collectVo.setId(collect.getId());
            collectVo.setType(collect.getType());
            collectVo.setRetailPrice(goods.getRetailPrice());
            result.add(collectVo);
        }

        return result;
    }

    @Override
    public void addOrDelete(Byte type, Integer valueId, Integer userId) {


        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.or().andTypeEqualTo(type).andValueIdEqualTo(valueId).andUserIdEqualTo(userId);
        MarketCollect collect = collectMapper.selectOneByExample(collectExample);

        if (collect == null){
            // 说明第一次收藏
            MarketCollect add = new MarketCollect();
            add.setUserId(userId);
            add.setValueId(valueId);
            add.setType(type);
            add.setAddTime(LocalDateTime.now());
            add.setUpdateTime(LocalDateTime.now());
            add.setDeleted(false);
            collectMapper.insertSelective(add);

        }else if (collect.getDeleted()){
            //  之前被删除
            collect.setDeleted(false);
            collectMapper.updateByExampleSelective(collect,collectExample);
        }else {
            // 删除收藏
            collect.setDeleted(true);
            collectMapper.updateByExampleSelective(collect,collectExample);
        }
    }
}
